package vm.baseapi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class Shell {
    public static void executeCommand(String Command) throws Exception {
        String osName = System.getProperty("os.name").toLowerCase();
        Process process = null;
        try {
            if (osName.contains("win")) {
                // Windows
                process = Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", Command});
            } else {
                // Linux or macOS
                process = Runtime.getRuntime().exec(new String[] {"bash" , "-c" , Command});
            }

            //OutputStream outputStream =process.getOutputStream();
            // 读取命令执行结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            // 等待命令执行完成
            //int exitCode = process.waitFor();
            //System.out.println("Command executed with exit code: " + exitCode);
            //reader.close();
            //outputStream.close();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}